/**
 * Created by freethy on 2016/12/2.
 */



;
(function($){

    //input正在编辑的选项
    var input_option;
    var select_option;
    var date_option;
    var table_option;

    //根据field找到列index
    function getIndex(options,field){
        var columns = $(options.dg).bootstrapTable("getOptions").columns[0];
        var index = -1;
        var b = false;
        for (var i = 0; i < columns.length; i++) {
            if (columns[i].visible == false) {
                continue;
            }
            index++;
            if (columns[i].field == field) {
                b = true;
                break;
            }
        }
        if(b) {
            return index;
        } else {
          return -1;
        }
    }

    //input
    function bsInput(option){

        //获取列索引
        var col = getIndex(option,option.field);
        if(col < 0){
            return;
        }
        col++;

        //当前模板对象
        var tpl = $(option.tpl);

        //如果模板未初始化过 则初始化一次
        var init = tpl.attr("init");
        if(init != "1"){

            //绑定键盘按下事件
            tpl.on("keydown", function (event) {
                if (event.keyCode == 13 || event.keyCode == 27) {
                    tpl.trigger("blur");
                }
            });

            //绑定失去焦点事件
            tpl.on("blur", function (event) {
                console.log("编辑完成input");
                console.log(input_option.edit);

                var val = tpl.val();
                var data = $(input_option.dg).bootstrapTable("getData");
                var td = $(input_option.dg).find("tr[data-index=" + input_option.edit.row + "] td:nth-child(" + input_option.edit.col + ")");
                data[input_option.edit.row][input_option.field] = val;
                td.html(val);
                tpl.hide();

                if(typeof input_option.callback != "undefined"){
                    input_option.callback(input_option.edit.row,input_option.edit.col,val,input_option);
                }
            });

            //添加初始化完成标记
            tpl.attr("init","1");
            tpl.css("z-index",999);
            tpl.css("position","absolute");
            tpl.css("display","none");
        }

        //注册事件
        $(option.dg).on("click", "td:nth-child(" + col + ")", function () {

            var td = $(this);
            var row = td.parent().attr("data-index");

            //编辑前验证
            if(typeof option.before_edit != "undefined"){
                var e = option.before_edit(row,col);
                if(!e){
                    return;
                }
            }

            var pos = td.offset();
            var padH = tpl.innerWidth() - tpl.width();
            var padV = tpl.innerHeight() - tpl.height();
            var width = td.outerWidth() - padH - 2;
            var height = td.outerHeight() - padV - 2;
            var data = $(option.dg).bootstrapTable("getData");
            var val = data[row][option.field];

            //记录当前正在编辑的信息
            option.edit = {row:row,col:col,field:option.field};
            input_option = option;

            //设置模板信息
            tpl.css("left",(pos.left+1) + "px");
            tpl.css("top",(pos.top + 1) + "px");
            tpl.width(width);
            tpl.height(height);
            tpl.val(val);
            tpl.show();
            tpl.focus();

            //输出日志
            console.log("开始编辑input");
            console.log(option.edit);
        });

    }

    //select
    function bsSelect(option){

        //获取列索引
        var col = getIndex(option,option.field);
        if(col < 0){
            return;
        }
        col++;

        //模板对象
        var tpl = $(option.tpl);

        //如果未初始化 则初始化一次
        if(tpl.attr("init") != "1"){
            //注册keydown事件
            tpl.on("keydown", function (event) {
                if (event.keyCode == 13 || event.keyCode == 27) {
                    tpl.trigger("blur");
                }
            });

            //注册失去焦点事件
            tpl.on("blur", function () {
                console.log("编辑完成select");
                console.log(select_option.edit);

                var val = tpl.find("option:selected").text();
                var data = $(select_option.dg).bootstrapTable("getData");
                var td = $(select_option.dg).find("tr[data-index=" + select_option.edit.row + "] td:nth-child(" + select_option.edit.col + ")");
                data[select_option.edit.row][select_option.field] = val;
                td.html(val);
                tpl.hide();
                if(typeof select_option.callback != "undefined"){
                    select_option.callback(select_option.edit.row,select_option.edit.col,val,tpl,select_option);
                }
            });

            //添加初始化完成标记
            tpl.attr("init","1");
            tpl.css("z-index",999);
            tpl.css("position","absolute");
            tpl.css("display","none");
        }

        //注册事件
        $(option.dg).on("click", "td:nth-child(" + col + ")", function () {
            var td = $(this);
            var row = td.parent().attr("data-index");

            //编辑前验证
            if(typeof option.before_edit != "undefined"){
                var e = option.before_edit(row,col);
                if(!e){
                    return;
                }
            }

            var pos = td.offset();
            var padH = tpl.innerWidth() - tpl.width();
            var padV = tpl.innerHeight() - tpl.height();
            var width = td.outerWidth() - padH - 2;
            var height = td.outerHeight() - padV - 2;
            var val = td.html();

            //记录当前正在编辑的信息
            option.edit = {row:row,col:col,field:option.field};
            select_option = option;

            //设置模板信息
            tpl.css("left",(pos.left) + "px");
            tpl.css("top",(pos.top + 2) + "px");
            tpl.width(width);
            tpl.height(height);
            tpl.find("option").each(function(i,n){
                if(n.text == val) {
                    n.selected = true;
                }
            });
            tpl.show();
            tpl.focus();

            //输出日志
            console.log("开始编辑input");
            console.log(option.edit);
        });

    }

    //date
    function bsDate(option){
         //获取列索引
        var col = getIndex(option,option.field);
        if(col < 0){
            return;
        }
        col++;
        //当前模板对象
        var tpl = $(option.tpl);

        //如果模板未初始化过 则初始化一次
        var init = tpl.attr("init");
        if(init != "1"){

            //绑定键盘按下事件
            tpl.on("keydown", function (event) {
                if (event.keyCode == 13 || event.keyCode == 27) {
                    tpl.trigger("blur");
                    tpl.datepicker('hide');
                }
            });

            //绑定失去焦点事件
            tpl.on("blur", function (event) {
                console.log("编辑完成date");

                var val = tpl.val();
                var data = $(date_option.dg).bootstrapTable("getData");
                var td = $(date_option.dg).find("tr[data-index=" + date_option.edit.row + "] td:nth-child(" + date_option.edit.col + ")");
                data[date_option.edit.row][date_option.field] = val;
                td.html(val);
                tpl.hide();
                if(typeof date_option.callback != "undefined"){
                    date_option.callback(date_option.edit.row,date_option.edit.col,val,date_option);
                }
            });

            //绑定失去焦点事件
            tpl.on("hide", function (event) {
                console.log("编辑完成date");
                console.log(date_option.edit);

                var val = tpl.val();
                var data = $(date_option.dg).bootstrapTable("getData");
                var td = $(date_option.dg).find("tr[data-index=" + date_option.edit.row + "] td:nth-child(" + date_option.edit.col + ")");
                data[date_option.edit.row][date_option.field] = val;
                td.html(val);
                tpl.hide();
                if(typeof date_option.callback != "undefined"){
                    date_option.callback(date_option.edit.row,date_option.edit.col,val,date_option);
                }
            });

            //添加初始化完成标记
            tpl.attr("init","1");
            tpl.css("z-index",999);
            tpl.css("position","absolute");
            tpl.css("display","none");
        }

        //注册事件
        $(option.dg).on("click", "td:nth-child(" + col + ")", function () {
            var td = $(this);
            var row = td.parent().attr("data-index");

            //编辑前验证
            if(typeof option.before_edit != "undefined"){
                var e = option.before_edit(row,col);
                if(!e){
                    return;
                }
            }

            var pos = td.offset();
            var padH = tpl.innerWidth() - tpl.width();
            var padV = tpl.innerHeight() - tpl.height();
            var width = td.outerWidth() - padH - 2;
            var height = td.outerHeight() - padV - 2;
            var data = $(option.dg).bootstrapTable("getData");
            var val = data[row][option.field];

            //记录当前正在编辑的信息
            option.edit = {row:row,col:col,field:option.field};
            date_option = option;

            //设置模板信息
            tpl.css("left",(pos.left+1) + "px");
            tpl.css("top",(pos.top + 1) + "px");
            tpl.width(width);
            tpl.height(height);
            tpl.val(val);
            tpl.show();
            tpl.focus();

            tpl.datepicker('update');

            //输出日志
            console.log("开始编辑date");
        });
    }

    //table
    function bsTable(option){
        //获取列索引
        var col = getIndex(option,option.field);
        console.log("bsTable col " + col);
        if(col < 0){
            return;
        }
        col++;
        //当前模板对象
        var tpl = $(option.tpl);

        //如果模板未初始化过 则初始化一次
        var init = tpl.attr("init");
        if(init != "1"){

            //绑定选择事件
            tpl.on("onSelect", function (event,id,txt,rowData,fdata) {
                var data = $(table_option.dg).bootstrapTable("getData");
                data[table_option.edit.row][table_option.field] = txt;
                var td = $(table_option.dg).find("tr[data-index=" + table_option.edit.row + "] td:nth-child(" + table_option.edit.col + ")");
                td.html(txt);
                if(typeof table_option.callback1 != "undefined"){
                    table_option.callback1(table_option.edit.row,table_option.edit.col,rowData);
                }
            });

            //绑定取消选择事件
            tpl.on("onUnSelect", function () {
                var val = tpl.val();
                var data = $(table_option.dg).bootstrapTable("getData");
                data[table_option.edit.row][table_option.field] = val;
                if(typeof table_option.callback2 != "undefined"){
                    table_option.callback2(table_option.edit.row,table_option.edit.col);
                }
            });

            //绑定编辑器隐藏事件
            tpl.on("onHide", function () {
                    tpl.hide();
                if(typeof table_option.callback != "undefined"){
                    table_option.callback(table_option.edit.row,table_option.edit.col);
                }
            });

            //添加初始化完成标记
            tpl.attr("init","1");
            tpl.css("z-index",999);
            tpl.css("position","absolute");
            tpl.css("display","none");
        }

        //注册事件
        $(option.dg).on("click", "td:nth-child(" + col + ")", function () {
            var td = $(this);
            var row = td.parent().attr("data-index");

            //编辑前验证
            if(typeof option.before_edit != "undefined"){
                var e = option.before_edit(row,col);
                if(!e){
                    return;
                }
            }

            var pos = td.offset();
            var padH = tpl.innerWidth() - tpl.width();
            var padV = tpl.innerHeight() - tpl.height();
            var width = td.outerWidth() - padH - 2;
            var height = td.outerHeight() - padV - 2;
            var data = $(option.dg).bootstrapTable("getData");
            var val = data[row][option.field];

            //记录当前正在编辑的信息
            option.edit = {row:row,col:col,field:option.field};
            table_option = option;

            //设置模板信息
            tpl.css("left",(pos.left+1) + "px");
            tpl.css("top",(pos.top + 1) + "px");
            tpl.width(width);
            tpl.height(height);
            tpl.val(val);
            tpl.show();
            tpl.focus();
            tpl.trigger('click');

            //输出日志
            console.log("开始编辑table");
            console.log(option.edit);
        });

    }

    return $.fn.bsEdit = function(options){
        if(options.type == "input"){
            bsInput(options);
        }else if(options.type == "select"){
            bsSelect(options);
        }else if(options.type == "date"){
            bsDate(options);
        }else if(options.type == "table"){
            bsTable(options);
        } else {
            console.log("不支持的类型");
        }
    }

})($);


